package com.lcy.web.base

import org.jboss.resteasy.spi.StringParameterUnmarshaller
import org.jboss.resteasy.util.FindAnnotation
import java.text.SimpleDateFormat
import java.util.*
import javax.ws.rs.ext.Provider

/**
 * @author loucy
 * @version 1.0
 */
@Provider
class DataParamFormat :StringParameterUnmarshaller<Date>{
    var formatter : SimpleDateFormat? = null
    override fun setAnnotations(p0: Array<out Annotation>?) {
        val formatAnnotation = FindAnnotation.findAnnotation(p0,DateFormat::class.java)
        formatter = SimpleDateFormat(formatAnnotation.value)
    }

    override fun fromString(p0: String?): Date? {
        try {
            if(formatter == null){
                formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            }
            return formatter?.parse(p0)
        } catch(e: Exception) {
        }
        return null
    }
}